home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 20 code / Pict Tricks / Batch CLUTLess / Shell Files / utils.c < prev   
Encoding:
Text File  |  1994-10-14  |  4.6 KB  |  192 lines  |  [TEXT/MMCC]

  1. //---------------------------------------------------------------------
  2. //---------------------------------------------------------------------
  3. //
  4. //    Horrible Rickety Shell, by Dave Johnson
  5. //
  6. //    © Copyright 1985 - 1994 Anyone Who Wants It,
  7. //    All Rights Energetically Heaved as far away from me as possible.
  8. //    Use at your own (considerable) risk.
  9.  
  10.  
  11. #include     "Shell.h"
  12. #include     <Traps.h>
  13.  
  14. extern  Boolean        doneflag;
  15.  
  16. /* These are stolen from arith.c, so we don't have to include the ANSI library for
  17. 2 routines
  18. int
  19. abs(int i)
  20. {
  21.     if (i < 0)
  22.         return(-i);
  23.     return(i);
  24. }
  25.  
  26.  
  27. long
  28. labs(long i)
  29. {
  30.     if (i < 0)
  31.         return(-i);
  32.     return(i);
  33. }
  34.   */
  35.  
  36. /****************************************************
  37. Quickdraw Things...
  38. *****************************************************/
  39.  
  40. /*------------------------------------------------------------------------
  41. NewPixImage()            Creates pix image and inits the fields of the pixmap...
  42. ------------------------------------------------------------------------*/
  43.  
  44. Boolean NewPixImage(PixMapHandle ThePix, Rect *TheRect, short dpth)
  45. {
  46.     Ptr                    ThePtr;
  47.     long                Offrowbytes, ptrsize;
  48.     
  49.     Offrowbytes    =(((dpth * (TheRect->right - TheRect->left)) + 15) / 16) * 2;
  50.     ptrsize = (TheRect->bottom - TheRect->top) * Offrowbytes;
  51.     ThePtr = NewPtr(ptrsize);
  52.     if(MemError() != noErr)
  53.         return(false);
  54.     
  55.     (**ThePix).baseAddr = ThePtr;
  56.     (**ThePix).rowBytes = Offrowbytes + 0x8000;
  57.     (**ThePix).bounds = *TheRect;
  58.     (**ThePix).pixelSize = dpth;
  59.     (**ThePix).cmpCount = 1;
  60.     (**ThePix).cmpSize = dpth;
  61.     return(true);
  62. }
  63.     
  64.  
  65. /*------------------------------------------------------------------------
  66. NewBitMap()            Creates bit image and inits the fields of the bitmap...
  67. ------------------------------------------------------------------------*/
  68.  
  69. Boolean NewBitMap(BitMap *TheMap, Rect *TheRect)
  70. {
  71.     long    rb;
  72.     Ptr        ba;
  73.     
  74.     rb = ((TheRect->right - TheRect->left + 15) / 16) * 2;
  75.     ba = NewPtr(rb * (TheRect->bottom - TheRect->top));
  76.     if( MemError() == noErr)
  77.     {
  78.         TheMap->rowBytes = rb;
  79.         TheMap->baseAddr = ba;
  80.         TheMap->bounds = *TheRect;
  81.         return(true);
  82.     }
  83.     else
  84.         return(false);
  85. }
  86.  
  87. /*-------------------------------------------------------------------------
  88. CenterRect()             Centers theRect over thePt...
  89. --------------------------------------------------------------------------*/
  90. void CenterRect(Rect *theRect, Point *thePt)
  91. {
  92.     /* First home theRect... */
  93.     
  94.     OffsetRect(theRect, -theRect->left, -theRect->top);
  95.     
  96.     /* ...then center it over thePt */
  97.     
  98.      
  99.     thePt->h = thePt->h - (theRect->right / 2);
  100.     thePt->v = thePt->v - (theRect->bottom / 2);
  101.     OffsetRect(theRect, thePt->h, thePt->v);
  102. }
  103.  
  104. /*-------------------------------------------------------------------------
  105. Center()             Returns the center of the rect
  106. --------------------------------------------------------------------------*/
  107. Point Center(Rect *theRect)
  108. {
  109.     Point        pt;
  110.         
  111.     pt.h = theRect->left + ((theRect->right - theRect->left) / 2);
  112.     pt.v = theRect->top + ((theRect->bottom - theRect->top) / 2);
  113.     return(pt);
  114. }
  115.  
  116.  
  117. /*-------------------------------------------------------------------------
  118. These routines are from DTS Utilities
  119. --------------------------------------------------------------------------*/
  120. short    NumToolboxTraps(void)
  121. {
  122.     if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  123.         return (0x200);
  124.     else
  125.         return (0x400);
  126. }
  127.  
  128. TrapType    GetTrapType(short theTrap)
  129. {
  130.     /* OS traps start with A0, Tool with A8 or AA. */
  131.     if ((theTrap & 0x0800) == 0)                    /* per D.A. */
  132.         return (OSTrap);
  133.     else
  134.         return (ToolTrap);
  135. }
  136.  
  137. Boolean TrapAvailable(short theTrap)
  138. {
  139.     TrapType    theTrapType;
  140.  
  141.     theTrapType = GetTrapType(theTrap);
  142.     if ((theTrapType == ToolTrap) && ((theTrap &= 0x07FF) >= NumToolboxTraps()))
  143.         theTrap = _Unimplemented;
  144.  
  145.     return (NGetTrapAddress(_Unimplemented, ToolTrap) != NGetTrapAddress(theTrap,
  146.                   theTrapType));
  147. }
  148.  
  149. // Catenate 2 Str255s
  150. void    pcat255(StringPtr d, StringPtr s)
  151. {
  152.     short    i, j;
  153.  
  154.     if (((j = s[0]) + d[0]) > 255)
  155.         j = 255 - d[0];
  156.             /* Limit dest string to 255. */
  157.  
  158.     for (i = 0; i < j;) d[++d[0]] = s[++i];
  159. }
  160.  
  161. // Catenate 2 Str63s
  162. void    pcat63(StringPtr d, StringPtr s)
  163. {
  164.     short    i, j;
  165.  
  166.     if (((j = s[0]) + d[0]) > 63)
  167.         j = 63 - d[0];
  168.             /* Limit dest string to 63. */
  169.  
  170.     for (i = 0; i < j;) d[++d[0]] = s[++i];
  171. }
  172.  
  173. // Catenate 2 Strs, but make the result <= 31 characters, with s guaranteed to
  174. // be at the end
  175. void    AppendStr31(StringPtr d, StringPtr s)
  176. {
  177.     short    i, addLength = s[0], posIndex = d[0];
  178.     
  179.     // Keep suffix a reasonable length
  180.     if(addLength > 20)
  181.         addLength = 20;
  182.     
  183.     // Limit ending srting to 31
  184.     if ((posIndex + addLength) > 31)
  185.         posIndex = 31 - addLength;
  186.     
  187.     // Append
  188.     for (i = 0; i < addLength;) d[++posIndex] = s[++i];
  189.     d[0] = posIndex; // remember new length
  190. }
  191.  
  192.